/**
 * Copyright (c) 2010 Abadi Kurniawan, Brian Dukes
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package hackathon.android.mymessageinbox;

import java.text.DateFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

/**
 * @author Abadi Kurniawan, Brian Dukes
 *
 */
public class ViewerActivity extends Activity {
	public static final String EXTRA_MESSAGE = "message";
	public static final String EXTRA_MESSAGE_LIST = "messageList";
	public static final String EXTRA_MESSAGE_POSITION = "messageIndex";
	
	private int position;
	private ArrayList<Message> list; 

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		setContentView(R.layout.viewer);
		
		Message msg = (Message)getIntent().getExtras().get(EXTRA_MESSAGE);
		list = (ArrayList<Message>)getIntent().getExtras().get(EXTRA_MESSAGE_LIST);
		position = getIntent().getExtras().getInt(EXTRA_MESSAGE_POSITION);
		
		displayMessage(msg);
		
		Button prevButton = ((Button)findViewById(R.id.viewer_previous_button));
		Button nextButton = ((Button)findViewById(R.id.viewer_next_button));
		if(position == 0) {
			prevButton.setEnabled(false);
		}
		prevButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Message prevMsg = list.get(--position);
					displayMessage(prevMsg);
					setNavigationButton();
					Log.d("Engage","[position:"+position+"]");
				}				
			});
		
		if(position == list.size() - 1)
		{
			nextButton.setEnabled(false);
		}
		nextButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Message nextMsg = list.get(++position);
					displayMessage(nextMsg);
					setNavigationButton();
					Log.d("Engage","[position:"+position+"]");
				}				
			});		
	}
	
	private void setNavigationButton()
	{
		Button prevButton = ((Button)findViewById(R.id.viewer_previous_button));
		Button nextButton = ((Button)findViewById(R.id.viewer_next_button));

		if (position == list.size() - 1)
		{
			nextButton.setEnabled(false);
		}else {
			nextButton.setEnabled(true);
		}

		if (position == 0)
		{
			prevButton.setEnabled(false);
		}else {
			prevButton.setEnabled(true);
		}
	}
	
	private void displayMessage(Message msg)
	{
		((TextView)findViewById(R.id.viewer_from_text)).setText(msg.getSender());
		((TextView)findViewById(R.id.viewer_to_text)).setText((msg.getToUserName() != null)? msg.getToUserName(): msg.getToRole());
		((TextView)findViewById(R.id.viewer_date_text)).setText(DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(msg.getDate()));
		((TextView)findViewById(R.id.viewer_subject_text)).setText(msg.getSubject());
		WebView body = ((WebView)findViewById(R.id.viewer_body_text));
		body.clearView();
		body.loadData(msg.getBody(), "text/html", "utf-8");
	}
}
